package com.moioo.util.edit;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class UploadWebHelper {
	public String filename = "D:/style2.xml";
	Document document = null;
	EditWebhelper ew = null;
	private static UploadWebHelper instance = null;
	private UploadBean bean = null;

	private UploadWebHelper() {
	}

	public static UploadWebHelper getInstance(ServletConfig config) {
		if (instance == null) {
			try {
				instance = new UploadWebHelper();
				instance.filename = config.getServletContext().getRealPath("/") + "WEB-INF/style.xml";
				SAXReader saxReader = new SAXReader();
				instance.document = saxReader.read(new File(instance.filename));
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
		return instance;
	}

	public UploadBean InitPara(javax.servlet.http.HttpServletRequest request) {
		String sToolBar = "";
		if (bean == null) {
			try {
				bean = new UploadBean();
				List list = document.selectNodes("Edit_Style/style");
				bean.setSfileext(getNodeValue(list, "sfileext"));
				bean.setSfilesize(getNodeValue(list, "sfilesize"));
				bean.setSflashext(getNodeValue(list, "sflashext"));
				bean.setSflashsize(getNodeValue(list, "sflashsize"));
				bean.setSimageext(getNodeValue(list, "simageext"));
				bean.setSimagesize(getNodeValue(list, "simagesize"));
				bean.setSmediaext(getNodeValue(list, "smediaext"));
				bean.setSmediasize(getNodeValue(list, "smediasize"));
				bean.setSremoteext(getNodeValue(list, "sremoteext"));
				bean.setSremotesize(getNodeValue(list, "sremotesize"));
				bean.setSuploaddir(getNodeValue(list, "suploaddir"));
				String sType = request.getParameter("type");
				String sAllowExt;
				int nAllowSize;
				if (sType.equalsIgnoreCase("remote")) {
					sAllowExt = bean.getSremoteext();
					sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
					nAllowSize = Integer.parseInt(bean.getSremotesize());
					System.out.println(sAllowExt + nAllowSize);
				} else if (sType.equalsIgnoreCase("file")) {
					sAllowExt = bean.getSfileext();
					sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
					nAllowSize = Integer.parseInt(bean.getSfilesize());
					System.out.println(sAllowExt + nAllowSize);
				} else if (sType.equalsIgnoreCase("media")) {
					sAllowExt = bean.getSmediaext();
					sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
					nAllowSize = Integer.parseInt(bean.getSmediasize());
					System.out.println(sAllowExt + nAllowSize);
				} else if (sType.equalsIgnoreCase("flash")) {
					sAllowExt = bean.getSflashext();
					sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
					nAllowSize = Integer.parseInt(bean.getSflashsize());
					System.out.println(sAllowExt + nAllowSize);
				} else {
					sAllowExt = bean.getSimageext();
					sAllowExt = sAllowExt + "|" + sAllowExt.toUpperCase();
					nAllowSize = Integer.parseInt(bean.getSimagesize());
					System.out.println(sAllowExt + nAllowSize);
				}
				bean.setSAllowExt(sAllowExt);
				bean.setNAllowSize(nAllowSize);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
			document = null;
		}
		return bean;
	}

	public String getNodeValue(List list, String Node) {
		Iterator it = list.iterator();
		if (it.hasNext()) {
			Element styleElement = (Element) it.next();
			Iterator memo = styleElement.elementIterator(Node);
			if (memo.hasNext()) {
				Element memostring = (Element) memo.next();
				return memostring.getTextTrim();
			} else {
				return "";
			}
		} else {
			return "";
		}
	}

	public String upFile(HttpServletRequest request, ServletOutputStream out) throws Exception {
		String str = "";
		String setExt[] = bean.getSAllowExt().split("[|]");
		//UploadImp u = new UploadImp(setExt,)
		return str;
	}
}
